package eg.backend.domain;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.datanucleus.jpa.annotations.Extension;

@Entity
@NamedQueries({
	@NamedQuery(name = "Tag.getByKey", query="select t from eg.backend.domain.Tag t where t.id=:id"),
	@NamedQuery(name = "Tag.getGlobalTag", query = "select t from eg.backend.domain.GlobalTag t where t.name = :name "),
	@NamedQuery(name = "Tag.findNotUsedByName", query = "select new Tag(g) from eg.backend.domain.GlobalTag g where g.name like '%' || :name || '%' and " +
			"not exists (select t from Tag t where t.usedBy = :owner and t.globalTag = g )"),
	
	@NamedQuery(name = "Tag.findByNameAndOwner", query = "select t.name from eg.backend.domain.Tag t where t.usedBy = :owner and t.name.startsWith(:name)"),
	@NamedQuery(name = "Tag.findByOwner", query = "select t.name from eg.backend.domain.Tag t where t.usedBy = :owner "),
	@NamedQuery(name="Tag.findAllGlobalTagNames", query="select t.name from eg.backend.domain.GlobalTag t "),
	
	@NamedQuery(name = "Tag.getByNameAndOwner", query = "select t from eg.backend.domain.Tag t where t.usedBy = :owner and t.name = :name "),
	@NamedQuery(name = "Tag.findUsedTags", query = "select new eg.backend.domain.fly.TagCount(t.globalTag.name, t.usedTimes) from eg.backend.domain.Tag t where t.usedBy = :owner order by t.usedTimes desc"),
	@NamedQuery(name="Tag.findAllGlobalTagNames", query="select t.name from eg.backend.domain.GlobalTag t "),
})
public class Tag {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
	private String id;

	private String globalTag;
	private String name;
	private String usedBy;
	
	private int usedTimes = 1;

	public Tag(String globalTag) {
		this.id = null;
		this.globalTag = globalTag;
	}
	
	public Tag(){}
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getGlobalTag() {
		return globalTag;
	}

	public void setGlobalTag(String globalTag) {
		this.globalTag = globalTag;
	}

	public void incrementUsedTimes() {
		this.usedTimes++;
	}
	
	public void decreaseUsedTimes() {
		this.usedTimes--;
	}

	public String getName() {
		return this.name;
	}
	
	public String getUsedBy() {
		return usedBy;
	}

	public void setUsedBy(String usedBy) {
		this.usedBy = usedBy;
	}

	public int getUsedTimes() {
		return usedTimes;
	}

	public void setUsedTimes(int usedTimes) {
		this.usedTimes = usedTimes;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (!this.getClass().isInstance(obj)) {
			return false;
		}

		Tag tag = (Tag) obj;
		return new EqualsBuilder().append(this.getUsedBy(), tag.getUsedBy()).append(this.getGlobalTag(), tag.getGlobalTag()).isEquals();	
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder().append(this.getUsedBy())
		.append(this.getGlobalTag()).toHashCode();
	}
	
}
